<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="社保信息ID" prop="insuranceInfoId" v-show="false">
      <el-input v-model="dataForm.insuranceInfoId" placeholder="社保信息ID"></el-input>
    </el-form-item>
    <el-form-item label="船员ID" prop="crewId" v-show="false">
      <el-input v-model="dataForm.crewId" placeholder="船员ID"></el-input>
    </el-form-item>
    <el-form-item label="姓名">
      <el-input v-model="crewName" placeholder="姓名" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="职务">
      <el-input v-model="crewPost" placeholder="职务" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="养老保险" prop="endowmentInsurance">
      <el-input v-model="dataForm.endowmentInsurance" placeholder="养老保险"></el-input>
    </el-form-item>
    <el-form-item label="医疗保险" prop="medicalInsurance">
      <el-input v-model="dataForm.medicalInsurance" placeholder="医疗保险"></el-input>
    </el-form-item>
    <el-form-item label="失业保险" prop="unemploymentInsurance">
      <el-input v-model="dataForm.unemploymentInsurance" placeholder="失业保险"></el-input>
    </el-form-item>
    <el-form-item label="工伤保险" prop="employmentInsurance">
      <el-input v-model="dataForm.employmentInsurance" placeholder="工伤保险"></el-input>
    </el-form-item>
    <el-form-item label="生育保险" prop="maternityInsurance">
      <el-input v-model="dataForm.maternityInsurance" placeholder="生育保险"></el-input>
    </el-form-item>
    <el-form-item label="公积金" prop="accumulationFund">
      <el-input v-model="dataForm.accumulationFund" placeholder="公积金"></el-input>
    </el-form-item>
    <el-form-item label="商业保险" prop="commercialInsurance">
      <el-input v-model="dataForm.commercialInsurance" placeholder="商业保险"></el-input>
    </el-form-item>
    <el-form-item label="其他" prop="otherInsurance">
      <el-input v-model="dataForm.otherInsurance" placeholder="其他"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        crewName: '',
        crewPost: '',
        visible: false,
        dataForm: {
          insuranceDetailsId: 0,
          insuranceInfoId: '',
          crewId: '',
          endowmentInsurance: '',
          medicalInsurance: '',
          unemploymentInsurance: '',
          employmentInsurance: '',
          maternityInsurance: '',
          accumulationFund: '',
          commercialInsurance: '',
          otherInsurance: ''
        },
        dataRule: {
          insuranceInfoId: [
            { required: true, message: '社保信息ID不能为空', trigger: 'blur' }
          ],
          crewId: [
            { required: true, message: '船员ID不能为空', trigger: 'blur' }
          ],
          endowmentInsurance: [
            { required: true, message: '养老保险不能为空', trigger: 'blur' }
          ],
          medicalInsurance: [
            { required: true, message: '医疗保险不能为空', trigger: 'blur' }
          ],
          unemploymentInsurance: [
            { required: true, message: '失业保险不能为空', trigger: 'blur' }
          ],
          employmentInsurance: [
            { required: true, message: '工伤保险不能为空', trigger: 'blur' }
          ],
          maternityInsurance: [
            { required: true, message: '生育保险不能为空', trigger: 'blur' }
          ],
          accumulationFund: [
            { required: true, message: '公积金不能为空', trigger: 'blur' }
          ],
          commercialInsurance: [
            { required: true, message: '商业保险不能为空', trigger: 'blur' }
          ],
          otherInsurance: [
            { required: true, message: '其他不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, crewName, crewPost) {
        this.crewName = crewName
        this.crewPost = crewPost
        this.dataForm.insuranceDetailsId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.insuranceDetailsId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/insurancedetails/info/${this.dataForm.insuranceDetailsId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.insuranceInfoId = data.insuranceDetails.insuranceInfoId
                this.dataForm.crewId = data.insuranceDetails.crewId
                this.dataForm.endowmentInsurance = data.insuranceDetails.endowmentInsurance
                this.dataForm.medicalInsurance = data.insuranceDetails.medicalInsurance
                this.dataForm.unemploymentInsurance = data.insuranceDetails.unemploymentInsurance
                this.dataForm.employmentInsurance = data.insuranceDetails.employmentInsurance
                this.dataForm.maternityInsurance = data.insuranceDetails.maternityInsurance
                this.dataForm.accumulationFund = data.insuranceDetails.accumulationFund
                this.dataForm.commercialInsurance = data.insuranceDetails.commercialInsurance
                this.dataForm.otherInsurance = data.insuranceDetails.otherInsurance
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/insurancedetails/${!this.dataForm.insuranceDetailsId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'insuranceDetailsId': this.dataForm.insuranceDetailsId || undefined,
                'insuranceInfoId': this.dataForm.insuranceInfoId,
                'crewId': this.dataForm.crewId,
                'endowmentInsurance': this.dataForm.endowmentInsurance,
                'medicalInsurance': this.dataForm.medicalInsurance,
                'unemploymentInsurance': this.dataForm.unemploymentInsurance,
                'employmentInsurance': this.dataForm.employmentInsurance,
                'maternityInsurance': this.dataForm.maternityInsurance,
                'accumulationFund': this.dataForm.accumulationFund,
                'commercialInsurance': this.dataForm.commercialInsurance,
                'otherInsurance': this.dataForm.otherInsurance
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
